#-*- coding: utf-8 -*-


from app import user
from flask import Flask,request,make_response,Response,jsonify,render_template

import json
import  sys

reload(sys)
sys.setdefaultencoding('utf8')

app = Flask(__name__)

@app.route('/',methods=['POST','GET'])
@app.route('/user/auth',methods=['POST','GET'])
def statnd_user_auth():
    if request.method == 'POST':
        args = request.form.to_dict()
        if 'phone' in args and 'passwd' in args:
            u = user.User()
            result = u.auth(**args)  # 字典传参
        else:
            result = {
                'code': 101,
                'message': 'please you post phone and passwd'
            }
        return jsonify(result)
    return render_template('login.html')


@app.route('/api/user/auth', methods=['POST', 'GET'])
def user_auth():
    if request.method == 'POST':
        args = request.form.to_dict()
        if 'phone' in args and 'passwd' in args:
            u = user.User()
            result = u.auth(**args)  # 字典传参
        else:
            result = {
                'code': 101,
                'message': 'please you post phone and passwd'
            }
    else:
        result = {
            'code': 100,
            'message': 'please use post method'
        }
    return jsonify(result)


# 注册接口
@app.route('/api/user/reg', methods=['POST', 'GET'])
def user_reg():
    if request.method == 'POST':
        args = request.form.to_dict()
        if 'phone' in args and 'passwd' in args and 'nickname' in args:
            u = user.User()
            try:
                result = u.reg(**args)  # 字典传参
            except Exception, e:
                result = {
                    'code': 102,
                    'message': e
                }

        else:
            result = {
                'code': 101,
                'message': 'please you post phone and passwd and nickname'
            }
    else:
        result = {
            'code': 100,
            'message': 'please use post method'
        }
    return jsonify(result)


# 查询用户接口
@app.route('/api/user/fetch_user', methods=['POST', 'GET'])
def fetch_user():
    if request.method == 'GET':
        args = request.args.to_dict()
        u = user.User()
        result = u.fetch_user(**args)  # 传课的类型与课期
    else:
        result = {
            'code': 101,
            'message': 'please you post phone and passwd and nickname'
        }
    return jsonify(result)


# 查询班级类型接口
@app.route('/api/user/fetch_class_type', methods=['POST', 'GET'])
def fetch_class_type():
    if request.method == 'GET':
        u = user.User()
        result = u.fetch_class_type()  # 传课的类型与课期
    else:
        result = {
            'code': 101,
            'message': 'please you post phone and passwd and nickname'
        }
    return jsonify(result)


# 查询班级期数接口
@app.route('/api/user/fetch_class_id', methods=['POST', 'GET'])
def fetch_class_id():
    if request.method == 'GET':
        u = user.User()
        result = u.fetch_class_id()  # 传课的类型与课期
    else:
        result = {
            'code': 101,
            'message': 'please you post phone and passwd and nickname'
        }
    return jsonify(result)


if __name__ == '__main__':
    app.run(host='192.168.0.9',port=9000,debug=True)
